import React from 'react';
import { Checkbox } from 'antd';
const CheckboxGroup = Checkbox.Group;

// const plainOptions = ['Apple', 'Pear', 'Orange'];
// const defaultCheckedList = ['Apple', 'Orange'];

class CheckboxComm extends React.Component {

    state = {
        checkAll: false,
        checkedList:this.props.checkedList,
        plainOptions:this.props.plainOptions,
        hidenplainOptions:this.props.hidenplainOptions,
        mainValue:this.props.mainValue,
        mainLabel:this.props.mainLabel
    };
    

    componentWillReceiveProps(props){
       this.setState({
            checkedList:this.props.checkedList,
            plainOptions:this.props.plainOptions
       })
    }
  
    render() {
     return (
      <div>
        <div style={{ borderBottom: '1px solid #E9E9E9' }}>
          <Checkbox
            onChange={this.onCheckAllChange}
            checked={this.state.checkAll}
            value ={this.state.mainValue}
          >
            {this.state.mainLabel}
          </Checkbox>
        </div>
        <br />
        <CheckboxGroup options={this.state.plainOptions} value={this.state.checkedList} onChange={this.onChange} />
      </div>
    );
  }
  onChange = (checkedList) => {
    this.setState({
      checkedList,
      checkAll: checkedList.length === this.state.hidenplainOptions.length,
    });
    var m = new Map();
    m.set("index", this.props.index)
    if(checkedList.length === this.state.hidenplainOptions.length){
      m.set("checkedList", checkedList+","+this.state.mainValue)
    }else{
      m.set("checkedList", checkedList);
    }
    this.props.getCheckedValue(m);
  }

  onCheckAllChange = (e,value) => {
    this.setState({
      checkedList: e.target.checked ? this.state.hidenplainOptions : [],
      checkAll: e.target.checked,
    });
    var m = new Map();
    m.set("index", this.props.index)
    if(e.target.checked){
      m.set("checkedList", this.state.hidenplainOptions+","+this.state.mainValue)
    }else{
      m.set("checkedList", []);
    }
    this.props.getCheckedValue(m);
  }
}

export default CheckboxComm;
